package com.lizhenyao.waimai.controller;

import com.google.common.collect.Maps;
import com.lizhenyao.waimai.converter.OrderForm2OrderDTOConverter;
import com.lizhenyao.waimai.dto.OrderDTO;
import com.lizhenyao.waimai.enums.ResultEnum;
import com.lizhenyao.waimai.exception.SellException;
import com.lizhenyao.waimai.form.OrderForm;
import com.lizhenyao.waimai.service.IBuyerService;
import com.lizhenyao.waimai.service.IOrderService;
import com.lizhenyao.waimai.utils.ResultVoUtil;
import com.lizhenyao.waimai.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @Author: lizhenyao
 * @Desciption:
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {

    @Autowired
    private IOrderService iOrderService;

    @Autowired
    private IBuyerService iBuyerService;

    //创建订单
    @PostMapping("/create")
    public ResultVO<Map<String, String>> create(@Valid OrderForm orderForm,
                                                BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            log.error("[创建订单] 参数不正确, orderForm = {}", orderForm);
            throw new SellException(ResultEnum.PARAM_ERROR.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }

        OrderDTO orderDTO = OrderForm2OrderDTOConverter.convert(orderForm);
        if (CollectionUtils.isEmpty(orderDTO.getOrderDetailList())){
            log.error("[创建订单] 购物车不能为空");
            throw new SellException(ResultEnum.CART_EMPTY);
        }
        //验证通过,创建订单
        OrderDTO resultOrderDTO = iOrderService.create(orderDTO);

        Map<String, String> resultMap = Maps.newHashMap();
        resultMap.put("orderId", resultOrderDTO.getOrderId());
        return ResultVoUtil.success(resultMap) ;
    }

    //订单列表
    @GetMapping("/list")
    public ResultVO<List<OrderDTO>> list(@RequestParam("openid") String openid,
                                         @RequestParam(value = "page", defaultValue = "0") Integer page,
                                         @RequestParam(value = "size", defaultValue = "10") Integer size){
        if (StringUtils.isBlank(openid)){
            log.error("[查询订单列表] openid为空");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }

        //参数验证通过
        PageRequest request = PageRequest.of(page, size);
        Page<OrderDTO> orderDTOPage = iOrderService.findList(openid, request);

        return ResultVoUtil.success(orderDTOPage.getContent());
    }


    //订单详情
    @GetMapping("/detail")
    public ResultVO<OrderDTO> detail(@RequestParam("openid") String openid,
                                     @RequestParam("orderId") String orderId){
        OrderDTO orderDTO = iBuyerService.findOrderOne(openid, orderId);
        return ResultVoUtil.success(orderDTO);
    }

    //取消订单
    @PostMapping("/cancel")
    public ResultVO<OrderDTO> cancel(@RequestParam("openid") String openid,
                                     @RequestParam("orderId") String orderId){

        OrderDTO orderDTO = iBuyerService.cancelOrder(openid, orderId);
        return ResultVoUtil.success();
    }
}
